package com.fjnu.controller;

import com.fjnu.dto.ArticleConditionDto;
import com.fjnu.dto.ArticleDto;
import com.fjnu.entity.Article;
import com.fjnu.uitl.ResponseResult;
import com.fjnu.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping("list")
    public ResponseResult queryArticles(ArticleConditionDto articleConditionDto){
        List<Article> list = articleService.list(articleConditionDto);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode("200");
        responseResult.setMessage("查询成功");
        responseResult.setData(list);
        return responseResult;
    }
    @PostMapping("add")
    public ResponseResult add(ArticleDto articleDto){
        int i = articleService.add(articleDto);
        ResponseResult responseResult = new ResponseResult();
        if (i > 0){
            responseResult.setCode("200");
            responseResult.setMessage("添加成功");
        }
        else{
            responseResult.setCode("500");
            responseResult.setMessage("添加失败");
        }
        return responseResult;
    }
    @GetMapping("{id}/publish")
    public ResponseResult publishArticle(@PathVariable("id") String articleId){
        int i = articleService.publish(articleId);
        ResponseResult responseResult = new ResponseResult();
        if (i > 0){
            responseResult.setCode("200");
            responseResult.setMessage("发布成功");
        }
        else{
            responseResult.setCode("500");
            responseResult.setMessage("发布失败");
        }
        return responseResult;
    }
    @PostMapping("update")
    public ResponseResult updateArticle(Integer id,ArticleConditionDto articleConditionDto){
        int i = articleService.updateArticle(id,articleConditionDto);
        ResponseResult responseResult = new ResponseResult();
        if (i > 0){
            responseResult.setCode("200");
            responseResult.setMessage("修改成功");
        }
        else{
            responseResult.setCode("500");
            responseResult.setMessage("修改失败");
        }
        return responseResult;
    }
}
